/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.utils;

import java.util.List;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1DummyInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;

public class FaceToFace {

	public static L1Character faceToFace(final L1PcInstance pc) {
		final int pcX = pc.getX();
		final int pcY = pc.getY();
		final int pcHeading = pc.getHeading();
		final List<L1Object> players = L1World.getInstance().getVisibleObjects(pc, 1);

		if (players.size() == 0) {// 1セル以内にPCが居ない場合
			pc.sendPackets(new S_ServerMessage(93)); // \f1そこには誰もいません。
			return null;
		}
		for (L1Object target : players) {
			L1Character cha = null;
			
			if(target instanceof L1PcInstance){
				cha = (L1PcInstance)target;
				
			}else if(target instanceof L1DummyInstance){
				cha = (L1DummyInstance)target;
			}else{
				continue;
			}
			
			final int targetX = cha.getX();
			final int targetY = cha.getY();
			final int targetHeading = cha.getHeading();
			if (pcHeading == 0 && pcX == targetX && pcY == targetY + 1) {
				if (targetHeading == 4) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 1 && pcX == targetX - 1
					&& pcY == targetY + 1) {
				if (targetHeading == 5) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 2 && pcX == targetX - 1 && pcY == targetY) {
				if (targetHeading == 6) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 3 && pcX == targetX - 1
					&& pcY == targetY - 1) {
				if (targetHeading == 7) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 4 && pcX == targetX && pcY == targetY - 1) {
				if (targetHeading == 0) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 5 && pcX == targetX + 1
					&& pcY == targetY - 1) {
				if (targetHeading == 1) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 6 && pcX == targetX + 1 && pcY == targetY) {
				if (targetHeading == 2) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			} else if (pcHeading == 7 && pcX == targetX + 1
					&& pcY == targetY + 1) {
				if (targetHeading == 3) {
					return cha;
				} else {
					pc.sendPackets(new S_ServerMessage(91, cha.getName())); // \f1%0があなたを見ていません。
					return null;
				}
			}
		}
		pc.sendPackets(new S_ServerMessage(93)); // \f1そこには誰もいません。
		return null;
	}

	private FaceToFace() {
	}
}
